
cc.Class({
    extends: cc.Component,

    properties: {
        icon: cc.Sprite,
        red: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},
    /**
     * 
     * @param {*} info 广告信息
     * @param {*} isShowRed 是否展示红点和晃动
     * @param {*} scale 节点大小
     */
    init(info, isShowRed, scale, call) {
        this.node.active = true
        // console.log(info)
        if (info) {
            this.loadImg(this.icon, info.show_config.image)
            this.crtId = info.creativeId
            this.posId = info.positionId
        }

        if (scale) {
            this.node.setScale(scale)
        } else {
            this.node.setScale(1)
        }
        this.scaleInit = scale || 1
        this.scaleSec = this.scaleInit * 1.1
        if (isShowRed >= 0) {
            this.red.active = isShowRed ? true : false
            this.scheduleServer()
        } else {
            this.red.active = false
        }



        if (call) {
            this.callFun = call
        }
        this.isCanClick = true
    },

    clear() {
        this.stopSchedule()
    },


    scheduleServer() {
        this.ani()
    },

    stopSchedule() {
        // this.unschedule(this.ani)
        if (this.tweenAni) {
            this.tweenAni.stop()
            this.tweenAni = null
        }
    },

    ani() {
        if (this.node) {
            this.tweenAni = cc.tween(this.node)
                .repeatForever(
                    cc.tween()
                        .to(0.2, { scale: this.scaleSec })
                        .to(0.1, { scale: this.scaleInit })
                        .to(0.2, { scale: this.scaleSec })
                        .to(0.1, { scale: this.scaleInit })
                        .call(() => {
                            this.node.setScale(this.scaleInit)
                        })
                        .delay(2)
                )
                .start()
        }

    },
    loadImg(container, url) {
        if (url.length < 4) {
            return he.error(("图片长度不对错误"));
        }
        let ext = url.substring(url.length - 4, url.length)
        ext = ext.toLowerCase()
        if (ext == 'jpeg') ext = '.jpg'
        cc.assetManager.loadRemote(url, { ext: ext }, (err, texture) => {
            if (err) {
                he.error(err)
            }
            if (this.callFun) {
                this.callFun()
            }
            var sprite = new cc.SpriteFrame(texture);
            container.spriteFrame = sprite;
        });
    },

    clilkServer() {
        if (this.posId && this.crtId) {
            if (wx.tmSDK && this.isCanClick) {
                this.isCanClick = false
                wx.tmSDK.flowNavigate({
                    positionId: this.posId, // 广告位id, 请先使用该id获取推广创意列表
                    creativeId: +this.crtId,  // 传入获取到的creativeId
                }).then((newList) => {
                    // console.log('跳转成功or取消跳转（可根据特有返回值区分两种情况）')
                    // console.log('自动刷新列表：', newList)//返回最新列表 
                    this.isCanClick = true
                }).catch((error) => {
                    // console.log('跳转失败', error);
                    this.isCanClick = true
                })
            }
        }
    },
});
